var app = getApp(), fetchApi = require("../../utils/httpPost.js")
import Dialog from '../../component/vant/dialog/dialog';
import Toast from '../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    favorites: [],
    netWork: true,
    hasData: false,
    isLoading: true,
    startX: 0,
    startY: 0,
    delBtnWidth: 60,
    closeItem: false,
    page: 1,
    last: false,
    pageSize:10,
    pageIndex:1,
    isxiaomi: app.globalData.isxiaomi,
    currentTab:0,
    collection_type:0,
    types:[0,1]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.onHttpInit(this.data.collection_type)
  },
  handleChange({ detail }) {
    console.log('detail',detail)
    this.data.pageIndex = 1;
    this.data.favorites = []
    this.setData({
      favorites : [],
      currentTab: detail.index
    })
    this.data.collection_type = this.data.types[detail.index];
    this.onHttpInit(detail.index);
  },
  initOp:function(){
    this.data.pageIndex = 1
    this.setData({
      currentTab: this.data.currentTab,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      favorites:[]
    })
    this.onHttpInit(this.data.collection_type)
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInit: function (collection_type) {
    var that = this
    fetchApi.httpPost('/user/collection/list', {
      collection_type: collection_type,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("收藏列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex+  1;
          var collections = res.data.data;
          if(collections.length > 0){
            for (var index in collections) {
              that.data.favorites.splice(that.data.favorites.length, 0, collections[index]);
            }
            that.setData({
              hasData: true,
              favorites: that.data.favorites
            })
          }
        }
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true, isLoading: false })
    }, function () {
        that.setData({ netWork: false, isLoading: false })
    })
  },
  pushToDetail: function (e) {
    var itemData = e.currentTarget.dataset.item;
    console.log("itemData", itemData)
    // 链接的类型   0 - 单商品id   1 - 商品页（title_menu_id）  2 - 团购商品id 3 - 团购商品页id  4 - 优惠券id
    if (itemData.collectionType == 0) {//
      wx.navigateTo({
        url: '/pages/product/detail/index?id=' + itemData.productId + '&section=0&from=collection'
      });
    }
  },
  bindDelete(e){
    console.log('goVerifyOrder e',e)
    var collectionId = e.currentTarget.dataset.collectionId;
    var that = this;
    var message = "是否删除该收藏？"
    Dialog.confirm({
      title: '删除收藏',
      message
    }).then(() => {
      that.onHttpDelete(collectionId)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  /**
    * 获取页面数据的 http请求
    */
  onHttpDelete: function (collect_id) {
      var that = this
      fetchApi.httpPost('/user/collection/delete', {
        collect_id: collect_id,
      }, function (res) {
        console.log("收藏删除数据", res)
        if (res.data.code == 0) {
          Toast.success(res.data.msg);
          that.initOp()
        }else{
          Toast.fail(res.data.msg);
        }
      }, function () {
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.initOp()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit(this.data.orderstate)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})